package com.g001.service;

import java.util.ArrayList;
import java.util.List;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

public class GruffaloIntro {
	
	private static final int SCREEN_WIDTH = 460;
	private Image introSlide;
	private Image treeBlur;
	private Image rayLight;
	private float x = 0;
	private float xTree = 0;
	private float xLightRay = 2500;
	private List<Tree> trees;
	private float minScrolling;
	private boolean dirRigthtoLeft = true;
	private final static float SPEED_SCROLLING = 0.02f;
	private final static float SCALE = 0.7f;
	private long timer;
	private final static long TIMERPAUSE = 3500l;
	private Image title;
	private boolean titleDisplay;
	private float scaleTtile = 1;
	private Color colorPulse;
	
	private boolean lightBrigther;
	private final static float ALPHA_TRANSPARENT = 0;
	private final static float ALPHA_OPAC = 1;
	
	private int positionY; 
	
	
	public void init() throws SlickException{
		introSlide = new Image("data/gruffalo/openingtitle.jpg");
		System.out.println(introSlide.getWidth()+"x"+introSlide.getHeight());
		introSlide = introSlide.getScaledCopy(SCALE);
		treeBlur = new Image("data/gruffalo/treeBlur.png");
		minScrolling = SCREEN_WIDTH - introSlide.getWidth();
		System.out.println(introSlide.getWidth()+"x"+introSlide.getHeight());
		trees = new ArrayList<Tree>();
		trees.add(new Tree(500,200,1.5f,treeBlur));
		trees.add(new Tree(1000,100,2,treeBlur));
		trees.add(new Tree(1745,-200,3,treeBlur));
		trees.add(new Tree(2200,-400,3,treeBlur));
		trees.add(new Tree(4500,-200,3,treeBlur));
		timer = System.currentTimeMillis() + TIMERPAUSE;
		title = new Image("data/gruffalo/title.png");
		title = title.getScaledCopy(SCALE);
		rayLight = new Image("data/gruffalo/lightRay.png");
		colorPulse = new Color(Color.white.r, Color.white.g, Color.white.b, Color.white.a);
		colorPulse.a = 0.5f;
		positionY = 258 + 10;
	}
	
	public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
		g.setClip(5,container.getHeight()-positionY,container.getWidth()-10,258);
		g.drawImage(introSlide, x,container.getHeight()-positionY);
		g.drawImage(rayLight, xLightRay,container.getHeight()-positionY,colorPulse);
		for(Tree v : trees) {
			g.drawImage(v.tree, v.x+xTree,v.y);
		}
		
//		if(titleDisplay){
//			g.drawImage(title.getScaledCopy(scaleTtile), 60 + ((1-scaleTtile) * title.getWidth())/2 , 380 + ((1-scaleTtile) * title.getHeight())/2);
//		}
		g.clearClip();
//		g.drawString("scrolling :"+x+"px dirRigthtoLeft:"+dirRigthtoLeft, 5, 200);
	}
	
	public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException {
		pulseLightRay(delta);
		if(timer<System.currentTimeMillis()) {
			if(dirRigthtoLeft) {
				scaleTtile = 1;
				if(x<minScrolling){
						dirRigthtoLeft = !dirRigthtoLeft;
						timer = System.currentTimeMillis() + TIMERPAUSE;
						titleDisplay = true;
				} else {
					x  = x - SPEED_SCROLLING * delta;
					xTree = xTree - 2*SPEED_SCROLLING * delta;
					xLightRay = xLightRay - 1.3f*SPEED_SCROLLING * delta;
				}
			} else {
				if(x>=0){
						dirRigthtoLeft = !dirRigthtoLeft;
						timer = System.currentTimeMillis() + TIMERPAUSE;
						titleDisplay = false;
						scaleTtile = 1;
				} else {
					x  = x + SPEED_SCROLLING * delta;
					xTree = xTree + 2*SPEED_SCROLLING * delta;
					xLightRay = xLightRay + 1.3f*SPEED_SCROLLING * delta;
				}
			}
		} else {
			scaleTtile = scaleTtile + 0.0001f * delta;
		}
	}

	
	private void pulseLightRay(int delta){
		
		if(lightBrigther){
			if(colorPulse.a<ALPHA_OPAC) {
				colorPulse.a = colorPulse.a+0.005f*SPEED_SCROLLING*delta;
			} else {
				lightBrigther = !lightBrigther;
			}
		} else {
			if(colorPulse.a>ALPHA_TRANSPARENT) {
				colorPulse.a = colorPulse.a-0.005f*SPEED_SCROLLING*delta;
			} else {
				lightBrigther = !lightBrigther;
			}
		}
		
		
		
	 
	}


public class Tree {
	
	public float x;
	public float y;
	public float scale;
	public Image tree;
	
	
	public Tree(final float x, final float y, final float scale, final Image image) {
		tree = image.getScaledCopy(scale);
		this.x = x;
		this.y = y;
		this.scale = scale;
		
	}
	
	
	
}

}
